﻿using System.Collections.Generic;
using System.Threading.Tasks;
using XCI.Core;
using XCI.Sys.Filter;
using XCI.Sys.Model;

namespace XCI.Sys.Service
{
    /// <summary>
    /// 系统文件服务
    /// </summary>
    public class SysFileService
    {
        private const string RootUrl = "/api/sys/file";
        public static SysFileService Instance { get; } = new SysFileService();

        /// <summary>
        /// 新建文件
        /// </summary>
        /// <param name="fileModel">上传文件模型对象</param>
        /// <param name="primaryKey">记录主键</param>
        /// <param name="tableName">表名</param>
        public Task InsertAsync(FileModel fileModel, string primaryKey, string tableName)
        {
            SysFile model = new SysFile
            {
                FileName = fileModel.Name,
                FilePath = fileModel.Url,
                FileSize = fileModel.Size,
                PrimaryKey = primaryKey,
                TableName = tableName,
                ContentType = fileModel.ContentType,
                Category = fileModel.Category
            };
            //model.Md5 = fileModel.Md5;
            return App.Client.PostAsync($"{RootUrl}/insert", model);
        }

        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="ids">文件主键字符串</param>
        public async Task DeleteAsync(string ids)
        {
            await App.Client.PostAsync($"{RootUrl}/delete", new { ids });
        }

        /// <summary>
        /// 根据记录主键删除文件
        /// </summary>
        /// <param name="primaryKey">记录主键</param>
        /// <param name="tableName">表名</param>
        public async Task DeleteByPrimaryKeyAsync(string primaryKey, string tableName)
        {
            await App.Client.PostAsync($"{RootUrl}/deleteByPrimaryKey", new { primaryKey, tableName });
        }

        /// <summary>
        /// 根据主键获取文件对象
        /// </summary>
        /// <param name="id">文件主键</param>
        public async Task<SysFile> SelectByIdAsync(string id)
        {
            return await App.Client.PostAsync<SysFile>($"{RootUrl}/selectById", new { id });
        }

        /// <summary>
        /// 根据记录主键获取第一个文件对象
        /// </summary>
        /// <param name="primaryKey">记录主键</param>
        /// <param name="tableName">表名</param>
        public async Task<SysFile> SelectFirstByPrimaryKeyAsync(string primaryKey, string tableName)
        {
            return await App.Client.PostAsync<SysFile>($"{RootUrl}/selectFirstByPrimaryKey", new { primaryKey, tableName });
        }

        /// <summary>
        /// 查询系统文件分页列表
        /// </summary>
        /// <param name="filter">过滤对象</param>
        public async Task<PagingList<SysFile>> SelectPageListAsync(SysFileFilter filter)
        {
            return await App.Client.PostAsync<PagingList<SysFile>>($"{RootUrl}/selectPageList", filter);
        }

        /// <summary>
        /// 查询系统文件列表
        /// </summary>
        /// <param name="filter">过滤对象</param>
        public async Task<List<SysFile>> SelectListAsync(SysFileFilter filter)
        {
            return await App.Client.PostAsync<List<SysFile>>($"{RootUrl}/selectList", filter);
        }

    }
}
